#!/bin/sh

# In case when mapped user id by `docker run -u` is not created inside docker image
# The `$HOME` variable points to `/` - which prevents any tool to write to, as it requires root access
# In such case we set `$HOME` to `/tmp` as it should r/w for everyone

if [ "$HOME" = "/" ] ; then
    export HOME=/tmp
fi

# In case of running as root, use `umask` to reduce problem of file permission on host
if [ "$(id -g)" = "0" ] && [ "$(id -u)" = "0" ] ;
then
    umask 0000
fi

# Export this image specific Environment variables
# Those variables are important to use dedicated folder for all cache and predefined config file
export EM_CONFIG=/emsdk/.emscripten
export EM_CACHE=/emsdk/.data/cache
export EM_PORTS=/emsdk/.data/ports

# Activate Emscripten SDK
. ${EMSDK}/emsdk_set_env.sh

# Evaluate a command that's coming after `docker run` / `docker exec`
"$@"
